//
//  AppDelegate.m
//  智慧小区
//
//  Created by lab603 on 15/6/19.
//  Copyright (c) 2015年 Dreamsoul. All rights reserved.
//

#import "AppDelegate.h"
#import "PPTabBarController.h"
#import "LoginViewController.h"
#import "IWNavigationController.h"
#import "AFNetworking.h"
#import "Tool.h"
#import "UMSocialWechatHandler.h"
#import "UMSocial.h"
#import "UMSocialQQHandler.h"
#import "NewfeatureViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //在程序加载完成时进行一些处理
    [self prepareForLaunchingWithOptions:launchOptions];
    //当程序是为启动时收到推送
    NSDictionary *userInfo = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
    if (userInfo) {
        //处理收到的推送信息
    }
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    //LoginViewController *loginVC = [[LoginViewController alloc] init];
    NewfeatureViewController *newfeatureVC = [[NewfeatureViewController alloc] init];
    //IWNavigationController *navigationVC = [[IWNavigationController alloc] initWithRootViewController:newfeatureVC];
    self.window.rootViewController = newfeatureVC;
    [self.window makeKeyAndVisible];
    
    return YES;
}
/**
 *  在程序完成加载后做一些准备
 */
- (void)prepareForLaunchingWithOptions:(NSDictionary *)launchOptions{
    //设置状态栏颜色
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    //设置分享平台内容
    [self setupShare];
    //让用户选择是否接受通知
    UIApplication *application = [UIApplication sharedApplication];
    [self userRecieveNoticeWithApp:application];
}
/**
 *  让用户选择是否接受通知
 */
- (void)userRecieveNoticeWithApp:(UIApplication *)application{
    // 注册通知服务
    if([UIDevice currentDevice].systemVersion.floatValue < 8.0)
    {
        [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge
                                                         | UIRemoteNotificationTypeSound
                                                         | UIRemoteNotificationTypeAlert)];
    }
    // IOS8.0以上版本的注册推送方式和以往不同
    else
    {
        UIUserNotificationSettings* settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
                                                                                             | UIRemoteNotificationTypeSound
                                                                                             | UIRemoteNotificationTypeAlert)
                                                                                 categories:nil];
        [application registerUserNotificationSettings:settings];
        [application registerForRemoteNotifications];
    }
}
/**
 *  设置分享平台内容
 */
- (void)setupShare{
    //设置友盟社会化组件appkey
    [UMSocialData setAppKey:UMENG_APPID];
    //打开调试log的开关
    [UMSocialData openLog:YES];
    //设置微信AppId，设置分享url，默认使用友盟的网址
    [UMSocialWechatHandler setWXAppId:@"wxf4a3d5f057983f77" appSecret:@"5211d21da336fd6bc77b09b3dee8cd93" url:@"http://www.umeng.com/social"];
    //设置分享到QQ空间的应用Id，和分享url 链接
    [UMSocialQQHandler setQQWithAppId:@"1104692525" appKey:@"i02NB1sklFO142iF" url:@"http://www.umeng.com/social"];
    //对没安装应用的分享进行隐藏
    [UMSocialConfig hiddenNotInstallPlatforms:@[UMShareToQQ, UMShareToQzone, UMShareToWechatSession, UMShareToWechatTimeline]];
    //设置支持没有客户端情况下使用SSO授权
    [UMSocialQQHandler setSupportWebView:YES];
}
/**
 *  用户允许接受推送后的处理
 */
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    NSString *tokenStr = [deviceToken description];
    NSString *pushToken = [[[tokenStr stringByReplacingOccurrencesOfString:@"<" withString:@""]stringByReplacingOccurrencesOfString:@">" withString:@""]
                            stringByReplacingOccurrencesOfString:@" " withString:@""];
    IWLog(@"%@",pushToken);
    //将token传回服务器端
}
/**
 *  用户不允许推送
 */
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    IWLog(@"%@",error);
}
/**
 *  程序接受到远程推送
 */
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    //处理推送消息
    IWLog(@"%@",userInfo);
    //应用在后台时接受到推送信息
    if (application.applicationState != UIApplicationStateActive) {
        NSLog(@"%@", userInfo);
    }
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
//    UIBackgroundTaskIdentifier task = [application beginBackgroundTaskWithExpirationHandler:^{
//        [application endBackgroundTask:task];
//    }];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    //把图标的数字清0
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
